﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ViewMaker;
using ViewMaker.Core.Tests;
using System.Diagnostics;

namespace SilverlightViewMaker.SampleApplication
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private List<WeakReference> viewList = new List<WeakReference>();
        private List<WeakReference> viewModelList = new List<WeakReference>();

        private void Button_Click1(object sender, RoutedEventArgs e)
        {

            var vm = new TestViewModel();
            var view = ViewUtil.BuildContent(vm);
            main.Content = view;

            viewList.Add(new WeakReference(view));
            viewModelList.Add(new WeakReference(vm));
        }
        private void Button_Click2(object sender, RoutedEventArgs e)
        {
            main.Content = ViewUtil.BuildContent(new SampleViewModel());
        }

        private void Button_Click3(object sender, RoutedEventArgs e)
        {
            main.Content = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Debug.WriteLine("Active View:" + viewList.Count(x => x.IsAlive));
            Debug.WriteLine("Active ViewModel:" + viewModelList.Count(x => x.IsAlive));
            Debug.WriteLine("TotalMemory:" + GC.GetTotalMemory(true).ToString());
        }

    }
}
